package com.bootdo.syn.service;

import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.utils.FileUtil;
import com.bootdo.common.utils.Hog;
import com.bootdo.common.utils.HttpUtil;
import com.bootdo.haojiangboutils.MapBuild;
import com.bootdo.syn.domain.SynFileTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.List;

//@Service
public class SynThreadService implements Runnable {

    private SynFileTableService synFileTableService;

    private BootdoConfig bootdoConfig;


    private long count = 0l;

    @Autowired
    public SynThreadService(
            BootdoConfig bootdoConfig,
            SynFileTableService synFileTableService){
        this.bootdoConfig = bootdoConfig;
        this.synFileTableService = synFileTableService;
        new Thread(this).start();
    }

    @Override
    public void run() {
        while (true){
            try {
                Thread.sleep(15000);
                Hog.e("文件考本线程等待执行.....");
                if(null != bootdoConfig && null != synFileTableService){
                    fileSynHander();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }




    void fileSynHander(){
         Long countTemp = synFileTableService.getByLastId();
         if(count != countTemp.longValue()){
             List<SynFileTable> synFileTableList =
                     synFileTableService.list(MapBuild.getInstand()
                             .setKV("notIp",bootdoConfig.getServerId()+","));
             if(null != synFileTableList && synFileTableList.size() > 0){

                 synFileTableList.stream().forEach(item ->{
                     synFile(item);
                 });
             }
             count = countTemp.longValue();
         }else{
            return;
         }
    }


    void  synFile(SynFileTable synFileTable){
        InputStream inputStream =   null;
        OutputStream outputStream = null;
        try {
            inputStream = HttpUtil.doGetByInputStream(synFileTable.getFileUrl());
            if(null != inputStream){
                byte [] b = new byte[1024];
                int len = 0;
                String filePath = synFileTable.getFileName().substring(0,  synFileTable.getFileName().lastIndexOf("/"));
                String fileName = synFileTable.getFileName().substring(synFileTable.getFileName().lastIndexOf("/")+1);
                Hog.e("filePath = {} fileName = {}",filePath,fileName);
                File targetFile = new File(bootdoConfig.getUploadPath()+filePath+"/");
                if (!targetFile.exists()) {
                    targetFile.mkdirs();
                }
                Hog.e("最后保存路径"+bootdoConfig.getUploadPath()+filePath+fileName);
                File file = new File(bootdoConfig.getUploadPath()+filePath+"/"+fileName);
                outputStream =  new  FileOutputStream(file);
                while ((len = inputStream.read(b)) != -1){
                    outputStream.write(b, 0, len);
                }
                outputStream.flush();
                synFileTable.setIp(synFileTable.getIp()+bootdoConfig.getServerId()+",");
                synFileTableService.save(synFileTable);
            }
        }catch (Exception e){
            // e.printStackTrace();
        }finally {
            try {
                if(null != inputStream){
                    inputStream.close();
                }
                if(null != outputStream){
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
